//#include"mgepch.h"
#include"Shader.h"
#include"Renderer.h"
#include"myMiniEngine/Platform/OpenGL/OpenGLShader.h"

#include<string>



namespace myMiniEngine {

	Ref<Shader> Shader::Create(const std::string& filepath)
	{
		switch (Renderer::GetAPI())
		{
		case RendererAPI::API::None:    MGE_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr;
		case RendererAPI::API::OpenGL:  return std::make_shared<OpenGLShader>(filepath);
		}

		MGE_CORE_ASSERT(false, "Unknown RendererAPI!");
		return nullptr;
		return nullptr;
	}
	Ref<Shader> Shader::Create(const std::string& name, const std::string& vertexSrc, const std::string& fragmentSrc) {
		switch (Renderer::GetAPI())
		{
		case RendererAPI::API::None:    MGE_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr;
		case RendererAPI::API::OpenGL:  return std::make_shared<OpenGLShader>(name,vertexSrc, fragmentSrc);
		}

		MGE_CORE_ASSERT(false, "Unknown RendererAPI!");
		return nullptr;
	}

	void ShaderLibrary::Add(const std::string _name, const Ref<Shader>& _shader)
	{
		MGE_CORE_ASSERT(!Exists(_name), "Shader does not exists!");
		shaders[_name] = _shader;
	}

	void ShaderLibrary::Add(const Ref<Shader>& _shader)
	{
		auto& _name = _shader->GetName();
		Add(_name, _shader);
	}

	

	Ref<Shader> ShaderLibrary::Load(const std::string& filepath)
	{
		auto _shader = Shader::Create(filepath);
		Add(_shader);
		return _shader;
	}

	Ref<Shader> ShaderLibrary::Load(const std::string& _name, const std::string& filepath)
	{
		auto _shader = Shader::Create(filepath);
		Add(_name,_shader);
		return _shader;	
	}

	Ref<Shader> ShaderLibrary::Get(const std::string& name)
	{
		MGE_CORE_ASSERT(Exists(name), "Shader not found!");
		return shaders[name];
	}

	bool ShaderLibrary::Exist(const std::string& _name) const
	{
		return shaders.find(_name) != shaders.end();
	}

}
